//
//  SJHomePageVC.m
//  SJPay
//
//  Created by 凯朱 on 2024/11/26.
//

#import "SJAuthVC.h"
#import "SJAuthView.h"
#import "SJAuthStep2View.h"
#import "SJAuthStep3View.h"
#import "LJContactManager.h"
#import "SJSubmitVC.h"
@interface SJAuthVC ()<TZImagePickerControllerDelegate,XZPickViewDelegate,XZPickViewDataSource,DatePickerViewDelegate>{
    UIImage *_icon1;
    UIImage *_icon2;
    UIImage *_icon3;
    UIImage *_icon4;
    NSArray *_pickerDatas;
    XZPickView *_picker;
    UIView *_dateBgView;
}
@property (nonatomic, strong)SJAuthView *authView;
@property (nonatomic, strong)SJAuthStep2View *authView2;
@property (nonatomic, strong)SJAuthStep3View *authView3;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)TZImagePickerController *imagePickerVc;
@end

@implementation SJAuthVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    kWeakSelf(self);
    [self.view addSubview:self.tableView];
    if (_step == 2) {
        self.title = kLocalString(@"工作信息");
        _authView2 = [SJAuthStep2View viewFromXIB];
        _authView2.frame = CGRectMake(0, 0, kScreenHeight, 710);
        self.tableView.tableHeaderView = _authView2;
        [_authView2 setBtnBlock:^(NSInteger btnIndex) {
            [weakself handleAuthView2Action:btnIndex];
        }];
        
    }else if (_step == 3){
        self.title = kLocalString(@"紧急联系人");
        _authView3 = [SJAuthStep3View viewFromXIB];
        _authView3.frame = CGRectMake(0, 0, kScreenHeight, 680);
        self.tableView.tableHeaderView = _authView3;
        [_authView3 setBtnBlock:^(NSInteger btnIndex) {
            [weakself handleAuthView3Action:btnIndex];
        }];
        
    }else{
        self.title = kLocalString(@"身份认证");
        _authView = [SJAuthView viewFromXIB];
        _authView.frame = CGRectMake(0, 0, kScreenHeight, 1020);
        self.tableView.tableHeaderView = _authView;
        [_authView setBtnBlock:^(NSInteger btnIndex) {
            [weakself handleAuthView1Action:btnIndex];
        }];
        [self configPickers:1];
    }
}

-(void)handleAuthView1Action:(NSInteger)index{
    if (index == 1 || index == 2 || index == 3) {//选择照片
        [self selectPhoto:index];
    }else if (index == 4){
        [_picker show];
    }else if (index == 5){
        [self.view endEditing:YES];
        [self showDateViewWith:1];
    }else if (index == 6){//下一步
        if (_icon1 == nil || _icon2 == nil || _icon3 == nil) {
            kShowToast(@"请上传照片");
            return;
        }
        if (_authView.tf1.text.length == 0) {
            kShowToast(@"请输入您的姓名");
            return;
        }
        if (_authView.tf2.text.length == 0) {
            kShowToast(@"选择性别");
            return;
        }
        if (_authView.tf3.text.length == 0) {
            kShowToast(@"请选择出生日期");
            return;
        }
        if (_authView.tf4.text.length == 0) {
            kShowToast(@"请输入证件号码");
            return;
        }
       
        SJAuthVC *vc = [SJAuthVC new];
        vc.step = 2;
        vc.applyFinish = _applyFinish;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)handleAuthView2Action:(NSInteger)index{
    if (index == 1) {
        [self configPickers:2];
        [_picker show];
    }else if (index == 2){
        [self configPickers:3];
        [_picker show];
    }else if (index == 3){//定位
        [ZCTool showHudOnView:self.view];
        [[TZLocationManager manager] startLocationWithGeocoderBlock:^(NSArray *geocoderArray) {
            [ZCTool hideHudOnView:self.view];
            if (geocoderArray.count == 0) {
                return;
            }
            CLPlacemark *placemark = geocoderArray[0];
            NSString *city = placemark.locality;
            if (!city) {
                //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
                city = placemark.administrativeArea;
            }
            NSLog(@"address = %@\n",placemark.name);
            NSDictionary *dictionary = [placemark addressDictionary];
            //  Country(国家)  State(省) locality（市） SubLocality(区)
            NSLog(@"%@%@%@",[dictionary objectForKey:@"State"], city,placemark.subLocality);
            self->_authView2.tf6.text = [NSString stringWithFormat:@"%@%@%@%@",[dictionary objectForKey:@"State"],city,placemark.subLocality,placemark.name];
        }];
    }else if (index == 4){//photo
        [self selectPhoto:4];
    }else if (index == 5){
        if (_authView2.tf1.text.length == 0) {
            kShowToast(@"请选择职业");
            return;
        }
        if (_authView2.tf2.text.length == 0) {
            kShowToast(@"请选择每月收入");
            return;
        }
        if (_authView2.tf5.text.length == 0) {
            kShowToast(@"请输入您的公司名称");
            return;
        }
        if (_authView2.tf6.text.length == 0) {
            kShowToast(@"请输入您的公司地址");
            return;
        }
        if (_authView2.tf7.text.length == 0) {
            kShowToast(@"请输入您的公司电话");
            return;
        }
        if (_icon4 == nil) {
            kShowToast(@"请上传证件照");
            return;
        }
        
        SJAuthVC *vc = [SJAuthVC new];
        vc.applyFinish = _applyFinish;
        vc.step = 3;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


-(void)handleAuthView3Action:(NSInteger)index{
    if (index == 1 || index == 2) {//通讯录
        [[LJContactManager sharedInstance] selectContactAtController:self complection:^(NSString *name, NSString *phone) {
            if (index == 1) {
                self->_authView3.tf2.text = name;
                self->_authView3.tf3.text = phone;
            }else{
                self->_authView3.tf6.text = name;
                self->_authView3.tf7.text = phone;
            }
            
        }];
    }else if (index == 3){//确定
        [self applyAction];
    }else if (index == 4){
        [self configPickers:4];
        [_picker show];
    }else if (index == 5){
        [self configPickers:5];
        [_picker show];
    }
}

-(void)applyAction{
    
    if (_authView3.tf1.text.length == 0 || _authView3.tf2.text.length == 0
        || _authView3.tf3.text.length == 0 || _authView3.tf4.text.length == 0
        || _authView3.tf5.text.length == 0 || _authView3.tf6.text.length == 0
        || _authView3.tf7.text.length == 0 || _authView3.tf8.text.length == 0) {
        kShowToast(@"请填写完整信息");
        return;
    }
    
    SJSubmitVC *vc = [SJSubmitVC new];
    vc.applyFinish = _applyFinish;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)showDateViewWith:(NSInteger)tag{
    if (_dateBgView) {
        return;
    }
    CGSize size = [[UIScreen mainScreen] bounds].size;
    _dateBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    _dateBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[[UIApplication sharedApplication] keyWindow] addSubview:_dateBgView];
    DatePickerView* datePickerView = [[DatePickerView alloc] initWithFrame:CGRectMake(0, KScreenHeight - 250, KScreenWidth, 250)];
    datePickerView.fundelegate = self;
    datePickerView.tag = tag;
    NSDate*now = [NSDate date];
    [datePickerView.datePicker setDate:now animated:YES];
    datePickerView.datePicker.frame = CGRectMake(0, 50, KScreenWidth, 200);
    [[[UIApplication sharedApplication] keyWindow] addSubview:datePickerView];
}

-(void)didDatePickerViewDoneClicked:(DatePickerView *)aPickerView{
    NSDate* date = [aPickerView selectDate];
    NSString *time = [NSString dateStrFromDate:date formatStr:@"yyyy-MM-dd"];
    _authView.tf3.text = time;
    [aPickerView removeFromSuperview];
    [_dateBgView removeFromSuperview];
    _dateBgView = nil;
}

- (void)didDatePickerViewCancelClicked:(DatePickerView*)aPickerView{
    [aPickerView removeFromSuperview];
    [_dateBgView removeFromSuperview];
    _dateBgView = nil;
}

-(void)configPickers:(NSInteger)index{
    if (index == 1) {
        _pickerDatas = @[kLocalString(@"男"),kLocalString(@"女")];
        _picker = [[XZPickView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) title:kLocalString(@"选择性别")];
    }else if(index == 2){
        _pickerDatas = @[kLocalString(@"技术人员"),kLocalString(@"工程师"),kLocalString(@"军人"),kLocalString(@"其它")];
        _picker = [[XZPickView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) title:kLocalString(@"选择职业")];
      
    }else if(index == 3){
        _pickerDatas = @[kLocalString(@"<5000"),kLocalString(@"5000~10000"),kLocalString(@"10000~20000"),kLocalString(@">20000")];
        _picker = [[XZPickView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) title:kLocalString(@"每月收入")];
    }else if(index == 4 || index == 5){
        _pickerDatas = @[kLocalString(@"配偶"),kLocalString(@"子女"),kLocalString(@"父母"),kLocalString(@"朋友")];
        _picker = [[XZPickView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) title:kLocalString(@"关系")];
    }
    _picker.tag = index;
    _picker.delegate = self;
    _picker.dataSource = self;
    
}

-(NSInteger)numberOfComponentsInPickerView:(XZPickView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(XZPickView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _pickerDatas.count;
}

-(void)pickView:(XZPickView *)pickerView confirmButtonClick:(UIButton *)button {
    NSInteger row = [pickerView selectedRowInComponent:0];
    if (pickerView.tag == 1) {
        _authView.tf2.text = _pickerDatas[row];
    }else if(pickerView.tag == 2){
        _authView2.tf1.text = _pickerDatas[row];
    }else if(pickerView.tag == 3){
        _authView2.tf2.text = _pickerDatas[row];
    }else if(pickerView.tag == 4){
        _authView3.tf1.text = _pickerDatas[row];
    }else if(pickerView.tag == 5){
        _authView3.tf5.text = _pickerDatas[row];
    }
    
    
  
}

- (NSString *)pickerView:(XZPickView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _pickerDatas[row];
}

-(void)selectPhoto:(NSInteger)index{
    
    _imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    
    _imagePickerVc.naviTitleFont = kFontMedium(20);
    _imagePickerVc.naviBgColor = KColor_B;
    _imagePickerVc.naviTitleColor = KWhiteColor;
    _imagePickerVc.barItemTextColor = KWhiteColor;
    _imagePickerVc.navigationController.title = kLocalString(@"选择照片");
    _imagePickerVc.barItemTextFont = kFontMedium(15);
    _imagePickerVc.allowPickingVideo = NO;
    _imagePickerVc.allowPickingGif = NO;
    _imagePickerVc.statusBarStyle = UIStatusBarStyleDefault;
  
    kWeakSelf(self);
    [_imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets,BOOL isSelectOriginalPhoto) {
        if (photos.count > 0) {
            [weakself updateSubViews:index image:photos[0]];
        }
    }];
    _imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:_imagePickerVc animated:YES completion:nil];
}

-(void)updateSubViews:(NSInteger)index image:(UIImage *)image{
    UIImageView *imgView;
    if (index == 1) {
        _icon1 = image;
        imgView = _authView.icon1;
    }else if (index == 2){
        _icon2 = image;
        imgView = _authView.icon2;
    }else if (index == 3){
        _icon3 = image;
        imgView = _authView.icon3;
    }else if (index == 4){
        _icon4 = image;
        imgView = _authView2.icon4;
    }
    
    if (imgView) {
        imgView.image = image;
    }
}


#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight,kScreenWidth,kScreenHeight-SafeAreaBottomHeight-SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KClearColor;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}






@end
